﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace eCompany
{
    public partial class shop_ctrl : UserControl
    {
        private eCompany.eObj.company m_comp = null;
        private string m_currency = null;
        private eCompany.eObj.currency_list m_cl = null;

        public shop_ctrl(eCompany.eObj.company comp, string currency, eCompany.eObj.currency_list cl)
        {
            InitializeComponent();

            m_comp = comp;
            m_currency = currency;
            m_cl = cl;
            RefreshShopList();
            RefreshTotalShopValue();
        }

        private void RefreshShopList()
        {
            eCompany.eManager.eManager eM = new eManager.eManager();
            shopListView.Items.Clear();
            eCompany.eObj.shop s = eM.getShop(m_comp, m_currency);

            foreach (eCompany.eObj.shop_stock st in s.stocks)
            {
                ListViewItem item = new ListViewItem();
                item.Text = st.date.ToString();
                item.SubItems.Add(st.volume_in.ToString());
                item.SubItems.Add(st.volume_out.ToString());
                item.SubItems.Add(eCompany.eManager.eManager.formatDouble(st.unit_price)+m_currency);
                item.SubItems.Add(eCompany.eManager.eManager.formatDouble(st.volume_in * st.unit_price)+m_currency);
                item.SubItems.Add(eCompany.eManager.eManager.formatDouble(eM.currencyToGOLD(m_cl, m_currency, (st.volume_in * st.unit_price))) + "G");
                if(st.volume_out<st.volume_in)
                    item.Font = new Font(item.Font.FontFamily, item.Font.Size, FontStyle.Bold);
                
                shopListView.Items.Add(item);
            }
        }

        private void RefreshTotalShopValue()
        {
            eCompany.eManager.eManager eM = new eManager.eManager();
            eCompany.eObj.shop s = eM.getShop(m_comp, m_currency);
            double shop_local_value = eM.getShopValue(s);
            double shop_count = eM.getShopStock(m_comp, m_currency);

            if (shop_count > 0)
                sellCmd.Enabled = true;
            else
                sellCmd.Enabled = false;

            lblShopLocalValue.Text = eCompany.eManager.eManager.formatDouble(shop_local_value) + s.currency;
            lblShopGoldValue.Text = eCompany.eManager.eManager.formatDouble(eM.currencyToGOLD(m_cl, s.currency, shop_local_value)) + "GLD";
        }

        private void sellCmd_Click(object sender, EventArgs e)
        {
            string str_units = "";

            if (Tools.InputBox("Sell", "Number of units", ref str_units) == DialogResult.OK)
            {
                double units = Convert.ToDouble(str_units);
                eCompany.eManager.eManager eM = new eManager.eManager();
                eM.sellShop(m_comp, m_currency, units);
                RefreshShopList();
                RefreshTotalShopValue();
            }
        }

        private void addShopCmd_Click(object sender, EventArgs e)
        {
            string str_units = "";
            string str_price = "";

            if (Tools.InputBox("Add Items to shop", "Volume", "Price", ref str_units, ref str_price) == DialogResult.OK)
            {
                System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US");
                str_units = str_units.Replace(",", ".");
                int units = Convert.ToInt32(str_units, culture);
                str_price = str_price.Replace(",", ".");
                double price = double.Parse(str_price, System.Globalization.NumberStyles.Number, culture);

                eCompany.eManager.eManager eM = new eManager.eManager();
                eM.addToShop(m_comp, m_currency, units, price);

                RefreshShopList();
                RefreshTotalShopValue();
            }
        }

        private void delShopCmd_Click(object sender, EventArgs e)
        {
            eCompany.eManager.eManager eM = new eManager.eManager();
            eM.removeFromShop(m_comp, m_currency);
            RefreshShopList();
            RefreshTotalShopValue();
        }
    }
}
